﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Collections;

using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using Microsoft.TeamFoundation.Server;

using LeMigue.Scrum.TfsScrumV1.Utils;

namespace LeMigue.Scrum.TfsScrumV1.Service
{

    public abstract class AbstractTfsService
    {

        private static TfsTeamProjectCollection _tfs;
        private static IGroupSecurityService _sec;

        public AbstractTfsService(){}

        public TfsTeamProjectCollection Tfs
        {
            get
            {
                if (_tfs == null)
                    _tfs = new TfsTeamProjectCollection(TfsTeamProjectCollection.GetFullyQualifiedUriForName(ConfigurationManager.AppSettings["tfsServer"]));

                return _tfs;
            }
        }
        internal IGroupSecurityService Sec
        {
            get
            {
                if (_sec == null)
                    _sec = GetService<IGroupSecurityService>();

                return _sec;
            }
        }
        public T GetService<T>()
        {
            return (T)Tfs.GetService(typeof(T));
        }


        public void Dispose()
        {
            if (_tfs != null)
            {
                _tfs.Dispose();
                _tfs = null;
            }

            GC.SuppressFinalize(this);
        }


        #region team projects
        
        protected ProjectCollection TeamProjets
        {
            get
            {
                WorkItemStore workItemStore = GetService<WorkItemStore>();
                return workItemStore.Projects;
            }
        }

        protected Project TeamProjeto(int id)
        {
            return TeamProjets.GetById(id);
        }
        #endregion

        #region work itens
        protected WorkItem GetItem(int idItem)
        {
            WorkItemStore workItemStore = GetService<WorkItemStore>();

            return workItemStore.GetWorkItem(idItem);
        }
        

        protected WorkItemCollection GetItens(TfsQuery query)
        {
            return GetItens(query.ToString(), query.Hashtable);
        }

        protected WorkItemCollection GetItens(string query, Hashtable hashtable)
        {
            WorkItemStore workItemStore = GetService<WorkItemStore>();

            WorkItemCollection retVal = workItemStore.Query(query, hashtable);

            return retVal;
        }
        

        public IList<WorkItem> GetItensByLinks(TfsQuery query)
        {

            return GetItensByLinks(query.ToString(), query.Hashtable);
        }

        public IList<WorkItem> GetItensByLinks(string query, Hashtable context)
        {
            IList<WorkItem> workItemList = new List<WorkItem>();
            WorkItemStore workItemStore = GetService<WorkItemStore>();

            Query wiQuery = new Query(workItemStore, query, context);

            WorkItemLinkInfo[] retVal = wiQuery.RunLinkQuery();

            foreach (WorkItemLinkInfo item in retVal)
            {
                if (item.TargetId != (int)context[WorkItemFields.SourceId.Key])
                    workItemList.Add(workItemStore.GetWorkItem(item.TargetId));
            }

            return workItemList;
        }

        
        public void Save(WorkItem item)
        {
            item.Save();
        }
        #endregion
        
    }
}
